package com.rm.zeyou.util.string;

import com.alibaba.fastjson.JSON;

/**
 * @author ZhangKeChen
 * @date 2022/9/5
 */
public class StringUtil {
    /**
     * 去除转义字符工具方法
     *
     * @param param
     * @return
     */
    public static String removeEscaping(String param) {
        return JSON.parse(param).toString();
    }

    /**
     * 将搜索的关键词拆分为单个字符 后执行 like 操作
     * @param searchName
     * @return
     */
    public static String search(String searchName) {
        String searchPattern = "%";
        for (char c : searchName.toCharArray()) {
            searchPattern += c + "%";
        }
        return searchPattern;
    }

    /**
     * 搜索关键词标红
     *
     * @param content 内容
     * @param keyword 搜索的关键词
     * @return
     */
    public static String highlightKeywordCharacters(String content, String keyword) {
        StringBuilder highlightedContent = new StringBuilder(content);

        int index = 0;

        for (char c : keyword.toCharArray()) {
            while (index < highlightedContent.length()) {
                if (highlightedContent.charAt(index) == c) {
                    highlightedContent.replace(index, index + 1, "<span style='color:red;'>" + c + "</span>");
                    break;
                }
                index++;
            }
            index = 0;
        }

        return highlightedContent.toString();
    }
}
